package com.zby.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.dto.ProductOrderDTO;
import com.zby.admin.pojo.vo.ProductOrderVo;
import com.zby.admin.service.ProductOrderService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class ProductOrderController {
    @Resource
    private ProductOrderService productOrderService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody ProductOrderDTO productOrderDTO, HttpServletRequest request) {
        boolean result = productOrderService.addData(productOrderDTO, request);
        if (!result) {
            return Result.error(500, "下单失败");
        }
        return Result.success();
    }

    /**
     * 获取个人下单
     *
     * @return
     */
    @GetMapping("/getPersonalOrder")
    public Result<List<ProductOrderVo>> getPersonalOrder(HttpServletRequest request) {
        List<ProductOrderVo> productOrder = productOrderService.getPersonalOrder(request);
        if (productOrder == null) {
            return Result.error(500, "获取用户信息失败");
        }
        return Result.success(productOrder);
    }

    /**
     * 个人卖出订单查询
     */
    @GetMapping("/getPersonalSellOrder")
    public Result<List<ProductOrderVo>> getPersonalSellOrder(@RequestParam Integer sallerId, HttpServletRequest request) {
        List<ProductOrderVo> productOrder = productOrderService.getPersonalSellOrder(sallerId, request);
        if (productOrder == null) {
            // 当用户未登录时，返回401未授权错误
            Object userId = request.getSession().getAttribute("userId");
            if (userId == null) {
                return Result.error(401, "用户未登录，请先登录");
            }
            // 当没有卖出订单时，返回空列表
            return Result.success(new ArrayList<>());
        }
        return Result.success(productOrder);
    }

    /**
     * 修改订单状态
     */
    @PostMapping("/updateOrderStatus")
    public Result<Boolean> updateOrderStatus(@RequestBody ProductOrderDTO productOrderDTO, HttpServletRequest request) {
        boolean result = productOrderService.updateOrderStatus(productOrderDTO, request);
        if (!result) {
            return Result.error(500, "修改失败");
        }
        return Result.success();
    }

    /**
     * 获取所有订单(分页+标题搜索)
     */
    @GetMapping("/getAllOrder")
    public Result<Page<ProductOrderVo>> getAllOrder(@RequestParam(defaultValue = "1") Integer page,
                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                    @RequestParam(required = false) String productTitle,
                                                    HttpServletRequest request) {
        // 判断是否登录
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }

        Page<ProductOrderVo> productOrder = productOrderService.getAllOrderList(page, pageSize, productTitle);
        return Result.success(productOrder);
    }

    /**
     * 获取状态为退货中的订单(分页)
     */
    @GetMapping("/getReturningOrder")
    public Result<Page<ProductOrderVo>> getReturningOrder(@RequestParam(defaultValue = "1") Integer page,
                                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest request) {
        // 获取用户ID
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }
        Page<ProductOrderVo> productOrder = productOrderService.getReturningOrder(page, pageSize);

        return Result.success(productOrder);
    }


}